﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShapeDemo
{
    class MyException:Exception
    {
        public MyException(string message):base(message) { }
    }
    abstract class Shape
    {
        public abstract double area();
        public abstract bool isLegal();
    }

    class Rectangle:Shape
    {
        private int height { get; set; }
        private int width { get; set; }

        public Rectangle(int height,int width)
        {
            this.height = height;
            this.width = width;
        }
        public override double area()
        {
            if (!isLegal())
            {
                throw new MyException("The rectangle is illegal.");
            }
            return height * width;
        }

        public override bool isLegal()
        {
            if(width>0&&height>0){
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    class Square : Shape
    {
        private int side { get; set; }

        public Square(int side)
        {
            this.side = side;
        }

        public override double area()
        {
            if (!isLegal())
            {
                throw new MyException("The Square is illegal.");
            }
            return side * side;
        }

        public override bool isLegal()
        {
            if (side>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    class Triangle : Shape
    {
        private int side1 { get; set; }
        private int side2 { get; set; }
        private int side3 { get; set; }

        public Triangle(int side1, int side2,int side3)
        {
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
        }
        public override double area()
        {
            if (!isLegal())
            {
                throw new MyException("The triangle is illegal.");
            }
            double p = (side1 + side2+side3)/2;
            return Math.Sqrt(p*(p-side1)*(p-side2)*(p-side3));
        }

        public override bool isLegal()
        {
            if (side1>0&&side2>0&&side3>0&&side1+side2>side3&&side1+side3>side2&&side2+side3>side1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Shape shape;
            Rectangle rectangle = new Rectangle(4, 5);
            shape = rectangle;
            try
            {
                Console.WriteLine(shape.area());
            }
            catch(MyException e)
            {
                Console.WriteLine(e.Message);
            }
            Square square = new Square(4);
            shape = square;
            try
            {
                Console.WriteLine(shape.area());
            }
            catch (MyException e)
            {
                Console.WriteLine(e.Message);
            }
            Triangle triangle = new Triangle(4, 5, 10);
            shape = triangle;
            try
            {
                Console.WriteLine(shape.area());
            }
            catch (MyException e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
    }
}
